package com.xuecheng.content.api;

import com.xuecheng.content.util.SecurityUtil;
import com.xuecheng.execption.ValidationGroups;
import com.xuecheng.execption.XueChengPlusException;
import com.xuecheng.model.PageParams;
import com.xuecheng.model.PageResult;
import com.xuecheng.model.dto.AddCourseDto;
import com.xuecheng.model.dto.CourseBaseInfoDto;
import com.xuecheng.model.dto.EditCourseDto;
import com.xuecheng.model.dto.QueryCourseParamsDto;
import com.xuecheng.model.po.CourseBase;
import com.xuecheng.service.CourseBaseInfoService;
import com.xuecheng.service.CourseCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;



/**
 * @description 课程信息编辑接口
 * @author 小班哥
 * @date 2023/3/11 11:29
 * @version 1.0
 */

@Api(value = "课程信息编辑接口" , tags = "课程信息编辑接口")
@RestController  //相当于Controller 和 responseBody
//@Slf4j
public class CourseBaseInfoController {

    @Autowired
    private CourseBaseInfoService courseBaseInfoService;

    @ApiOperation("课程查询接口")
    @PostMapping("/course/list")
    @PreAuthorize(value = "hasAnyAuthority('xc_teachmanager_course_list')")
    //@RequestBody是将传过来的数据转为JSON格式
    public PageResult<CourseBase> List(PageParams pageParams , @RequestBody(required = false) QueryCourseParamsDto queryCourseParamsDto){
        //拿到当前机构的ID
        String companyId = SecurityUtil.getUser().getCompanyId();
//        if (StringUtils.isEmpty(companyId)){//如果机构ID为空则抛出异常
//            throw new XueChengPlusException("获取课程信息失败");
//        }

        return courseBaseInfoService.queryCourseBaseList(Long.parseLong(companyId) , pageParams , queryCourseParamsDto);
    }


    @ApiOperation("新增课程基础信息")
    @PostMapping("/course")
    public CourseBaseInfoDto AddCourseBaseInfoDto(@RequestBody @Validated(value = {ValidationGroups.Insert.class}) AddCourseDto addCourseDto){
        //机构id，由于认证系统没有上线暂时硬编码
        Long companyId = 1232141425L;
        return courseBaseInfoService.createCourseBase( companyId ,addCourseDto);
    }

    @ApiOperation("根据ID查询课程基础信息")
    @GetMapping("/course/{courseId}")
    public CourseBaseInfoDto SelectByIDCourseBaseInfoDto(@PathVariable long courseId){
        //SecurityContextHolder
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        System.out.println(principal);
        return courseBaseInfoService.getCourseBaseInfo(courseId);
    }

    @ApiOperation("修改课程基本信息")
    @PutMapping("course")
    public CourseBaseInfoDto UpdateCourseBaseInfoDto(@RequestBody @Validated(value = {ValidationGroups.Update.class}) EditCourseDto editCourseDto){
        //机构id，由于认证系统没有上线暂时硬编码
        long id = 1232141425;
        return courseBaseInfoService.updateCourseBase(id , editCourseDto);
    }

    @ApiOperation("删除课程")
    @DeleteMapping(value = "/course/{courseID}")
    public void DeleteCourseInfo(@PathVariable long courseID){
        //机构id，由于认证系统没有上线暂时硬编码
        long id = 1232141425;
        courseBaseInfoService.DeleteCourseInfoByID(id , courseID);
    }



}
